<template>
  <div>
    <base-modal
      :title="$t('common.form.preview.sparepart.colum16')"
      :show.sync="visible"
      width="480px"
      :close-on-click-modal="false"
    >
      <div class="logistics-info-dialog">
        <div v-loading="loading">
          <div class="dialog-tab">
            <span
              v-for="(item, index) in parcelList"
              :key="item.number"
              :class="{ 'active-tab': currentIndex === index }"
              @click="handleChangeParcel(index)"
              >{{$t('order.component.packageText4', {data1: index + 1})}}</span
            >
          </div>
          <el-divider></el-divider>

          <div class="dialog-content" v-if="shopLogisticsType === 1">
            <div>{{ logisticName }}</div>
            <div class="dialog-content-btn">
              <span @click="handleGetLogisticsInfo">{{$t('order.component.queryLogisticsInfo')}}</span>
              <el-tooltip
                class="item"
                :content="$t('order.component.chargingStandard', {num1: '100'})"
                placement="top"
              >
                <i class="el-icon-warning-outline" style="margin-left: 4px" />
              </el-tooltip>
            </div>
            <template v-if="logisticList.length > 0">
              <el-timeline>
                <el-timeline-item
                  v-for="(activity, index) in logisticList"
                  :key="index"
                  :icon="activity.icon"
                  :type="activity.type"
                  :color="activity.color"
                  :size="activity.size"
                  :timestamp="activity.time"
                  placement="top"
                >
                  {{ activity.status }}
                </el-timeline-item>
              </el-timeline>
            </template>
            <div class="nothing" v-if="!logisticList.length && hasSearch">
              <img
                :src="imageNothing"
                alt=""
              />
              <div class="tip">
                {{$t('common.form.tip.logistics.tips6')}}
              </div>
            </div>
          </div>
          <div class="dialog-text" v-else-if="shopLogisticsType === 2">
            <i class="iconfont icon-beipinbeijian-2"></i>{{$t('order.component.noNeedLogistics')}}
          </div>
          <div class="dialog-text" v-else>
            <i class="iconfont icon-beipinbeijian-2"></i>{{$t('common.base.pickUp')}}
          </div>
        </div>
        <div class="nothing" v-if="!parcelList.length && !loading">
          <img :src="imageNothing" alt="" />
          <div class="tip">
            {{$t('common.form.tip.logistics.tips6')}}
          </div>
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
        <el-button type="primary" @click="visible = false">{{$t('common.base.makeSure')}}</el-button>
      </div>
    </base-modal>
  </div>
</template>

<script>
import {
  getSimpleLogistics,
  queryLogisticsDetails,
} from '@src/api/PortalApi.ts';
import { getOssUrl } from '@src/util/assets'
const imageNothing = getOssUrl('/form/form-logistics-nothing.png')
export default {
  data() {
    return {
      imageNothing,
      loading: false,
      visible: false,
      currentIndex: 0,
      orderId: '',
      parcelList: [],
      logisticList: [],
      hasSearch: false,
    };
  },
  computed: {
    curParcel() {
      return this.parcelList?.[this.currentIndex] || {};
    },
    shopLogisticsType() {
      return this.curParcel?.shopLogisticsType || 1;
    },
    logisticName() {
      let name = '';
      if (this.curParcel.logisticsCompany)
        name += `${this.curParcel.logisticsCompany} `;

      if (this.curParcel.logisticsNumber)
        name += this.curParcel.logisticsNumber;

      return name;
    },
  },
  methods: {
    open(id) {
      this.visible = true;
      this.orderId = id;
      this.logisticList = [];

      this.handleParcelInfo();
    },
    handleChangeParcel(index) {
      this.currentIndex = index;
      this.logisticList = [];
      this.hasSearch = false;
    },
    async handleParcelInfo() {
      try {
        this.loading = true;
        this.parcelList = [];
        this.hasSearch = false;
        const { code, data } = await getSimpleLogistics({
          orderId: this.orderId,
        });

        if (code !== '200' || !data) {
          this.loading = false;
          return this.$message.error(this.$t('order.tips.obtainlogistics'));
        }

        this.parcelList = data || [];
        this.loading = false;
      } catch (error) {
        this.loading = false;
        console.error('fetch logistics company list error', error);
      }
    },
    async handleGetLogisticsInfo() {
      try {
        this.loading = true;
        const { code, data } = await queryLogisticsDetails({
          orderId: this.orderId,
          logisticsCompany: this.curParcel.logisticsCompany,
          logisticsNumber: this.curParcel.logisticsNumber,
        });

        if (code !== '200') {
          this.loading = false;
          return this.$message.error(this.$t('order.tips.queryLogisticsInfoFailed'));
        }

        this.loading = false;
        this.hasSearch = true;
        this.logisticList = data?.list;
      } catch (error) {
        this.loading = false;
        console.error('fetch logistics info error', error);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.logistics-info-dialog {
  padding: 10px 0 30px;

  .dialog-tab {
    display: flex;
    align-items: center;
    margin-left: 20px;

    span {
      display: block;
      width: 53px;
      height: 28px;
      line-height: 28px;
      text-align: center;
      background: #fafafa;
      border-radius: 16px;
      color: #262626;
      font-size: 12px;
      cursor: pointer;
      margin-right: 12px;
    }

    .active-tab {
      background: #e8fafb;
      color: $color-primary-light-6;
    }
  }

  .dialog-content {
    padding: 0 30px;

    div {
      margin-bottom: 14px;
      font-size: 14px;
      color: #262626;
      font-weight: 500;
    }

    .el-timeline {
      padding: 0;
    }

    &-btn {
      span {
        color: $color-primary-light-6;
      }

      span,
      i {
        cursor: pointer;
      }
    }
  }

  .dialog-text {
    padding: 0 30px;

    i {
      margin-right: 5px;
    }
  }

  .el-divider {
    margin: 12px 0;
    background: #e5e5e5;
  }

  .nothing {
    text-align: center;
    img {
      max-width: 260px;
    }
    .tip {
      margin-top: 30px;
      font-size: 14px;
      font-weight: bold;
    }
  }
}
</style>
